package org.goldenport.g3.adapters

import org.goldenport.util.QSymbol
import org.goldenport.g3._
import org.goldenport.g3.messages._
import org.goldenport.g3.messages.event._
import org.goldenport.g3.events._
import org.goldenport.g3.events.event._

/**
 * @since   Aug. 31, 2010
 * @version Sep. 18, 2010
 * @author  ASAMI, Tomoharu
 */
class EventAdapter(val driver: G3EventDriver) extends G3Adapter {
  override def open_Adapter() {
    driver.open(context)
  }

  override def close_Adapter() {
    driver.close()
  }

  override protected def do_Action(content: AnyRef, parcel: G3Parcel, ctx: G3AgentContext): AnyRef = {
    content match {
      case cmd: G3Command => driver.execute(cmd, ctx)
    }
  }
}

abstract class G3EventDriver extends G3Driver {
  protected def new_Agent(cmd: G3Command, ctx: G3AgentContext): G3EventDriverAgent
}

abstract class G3EventDriverAgent(uri: String, context: G3AgentContext) extends G3DriverAgent(uri, context) with UseRecord {

  agent {
    case cmd: EventIssue => issue(cmd)
  }

  def issue(msg: EventIssue): G3Event = {
    issueTx(msg) match {
      case e: Exceptioned => e
      case evt: G3Event => {
        val rfs = issueRollforward(msg)
        val bes = issueBesteffort(msg)
        new EventIssued(uri, rfs, bes)
      }
    }
  }

  def issueTx(msg: EventIssue): G3Event

  def issueRollforward(msg: EventIssue): G3RollforwardResults = {
    new G3RollforwardResults()
  }

  def issueBesteffort(msg: EventIssue): G3BesteffortResults = {
    new G3BesteffortResults()
  }

  def get(get: Get): G3Event = {
    error("")
  }

  def post(post: Post): G3Event = {
    error("")
  }

  def put(put: Put): G3Event = {
    error("")
  }

  def delete(delete: Delete): G3Event = {
    error("")
  }

  def mkcol(mkcol: Mkcol): G3Event = {
    error("")
  }
}

abstract class G3DataStoreEventDriverAgent(uri: String, context: G3AgentContext) extends G3EventDriverAgent(uri, context) {
}

abstract class G3EntityEventDriverAgent(uri: String, context: G3AgentContext) extends G3EventDriverAgent(uri, context) {
  def issueTx(msg: EventIssue): G3Event = {
    error("XXX")
  }
}
